package music_store.ms.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.dao.CartMapper;
import music_store.ms.dao.MusicMapper;
import music_store.ms.pojo.Cart;
import music_store.ms.pojo.Music;
import music_store.ms.service.ICartService;
import music_store.ms.util.BigDecimalUtil;
import music_store.ms.vo.CartMusicVo;
import music_store.ms.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import music_store.ms.common.ServerResponse;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service("iCartService")
public class CartServiceImpl implements ICartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    MusicMapper musicMapper;

    public ServerResponse<CartVo> add (Integer userId, Integer musicId, Integer count){
        if(musicId == null || count == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Cart cart = cartMapper.selectCartByUserIdAndMusicId(userId, musicId);

        if (cart == null){
            //need to create a new cart
            Cart newCart = new Cart();
            newCart.setUserId(userId);
            newCart.setMusicId(musicId);
            newCart.setChecked(Const.Cart.CHECKED);
            newCart.setQuantity(count);
            cartMapper.insert(newCart);
        }
        else {
            //update music's quantity
            count += cart.getQuantity();
            cart.setQuantity(count);
            cartMapper.updateByPrimaryKeySelective(cart);
        }
        return this.list(userId);
    }

    public ServerResponse<CartVo> update (Integer userId, Integer musicId, Integer count){
        if(musicId == null || count == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Cart cart = cartMapper.selectCartByUserIdAndMusicId(userId, musicId);
        if (cart != null){
            cart.setQuantity(count);
        }
        cartMapper.updateByPrimaryKeySelective(cart);
        return this.list(userId);
    }

    public ServerResponse<CartVo> deleteMusic (Integer userId, String musicIds){
        List<String> musicIdList = Splitter.on(",").splitToList(musicIds);
        if (CollectionUtils.isEmpty(musicIdList)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        cartMapper.deleteByUserIdMusicIds(userId, musicIdList);
        return this.list(userId);
    }

    public ServerResponse<CartVo> selectOrUnselect(Integer userId, Integer musicId, Integer checked){
        cartMapper.checkedOrUnchecked(userId,checked,musicId);
        return this.list(userId);
    }

    public ServerResponse<CartVo> list(Integer userId){
        CartVo cartVo = getCartVo(userId);
        return ServerResponse.createBySuccess(cartVo);
    }

    //私有方法

    //返回userId指定的CartVo购物车的方法。
    private CartVo getCartVo(Integer userId){
        CartVo cartVo = new CartVo();

        List<Cart> cartList = cartMapper.selectCartByUserId(userId);
        List<CartMusicVo> cartMusicVoList = Lists.newArrayList();

        BigDecimal cartTotalPrice = new BigDecimal("0");

        if (!CollectionUtils.isEmpty(cartList)){
            for (Cart cartItem : cartList){
                CartMusicVo cartMusicVo = new CartMusicVo();

                //cart
                cartMusicVo.setId(cartItem.getId());
                cartMusicVo.setUserId(cartItem.getUserId());
                cartMusicVo.setMusicId(cartItem.getMusicId());
                cartMusicVo.setQuantity(cartItem.getQuantity());

                //music
                Music music = musicMapper.selectByPrimaryKey(cartItem.getMusicId());
                if (music != null){
                    cartMusicVo.setMusicName(music.getName());
                    cartMusicVo.setArtist(music.getArtist());
                    cartMusicVo.setAlbumName(music.getAlbumName());
                    cartMusicVo.setMusicPrice(music.getPrice());
                    cartMusicVo.setMusicStatus(music.getStatus());
                }

                //manage
                cartMusicVo.setMusicTotalPrice(BigDecimalUtil.mul(music.getPrice().doubleValue(),cartItem.getQuantity()));
                cartMusicVo.setMusicChecked(cartItem.getChecked()); //该商品是否被选中

                if (cartItem.getChecked() != null && Const.Cart.CHECKED == cartItem.getChecked() && cartMusicVo.getMusicTotalPrice() != null){
                    cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(), cartMusicVo.getMusicTotalPrice().doubleValue());
                }
                cartMusicVoList.add(cartMusicVo);   //往购物车中添加此商品。
            }
        }
        cartVo.setCartTotalPrice(cartTotalPrice);
        cartVo.setCartMusicVoList(cartMusicVoList);
        cartVo.setAllChecked(this.getAllCheckedStatus(userId));

        return cartVo;
    }

    private Boolean getAllCheckedStatus(Integer userId){
        if (userId == null){
            return false;
        }
        return cartMapper.selectCartMusicCheckedStatusByUserId(userId) == 0;
    }

}
